package com.ruoyi.quality.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.quality.domain.dto.InspectionTaskDto;
import com.ruoyi.quality.domain.dto.InspectionTaskTemplateSamplingItemDto;
import com.ruoyi.quality.domain.dto.InspectionTaskValueDto;
import com.ruoyi.quality.domain.vo.InspectionTaskVo;
import com.ruoyi.quality.service.InspectionTaskService;
import com.ruoyi.quality.service.InspectionTaskTemplateSamplingItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inspectionTask")
public class InspectionTaskController {
    @Autowired
    InspectionTaskService inspectionTaskService;

    @Autowired
    InspectionTaskTemplateSamplingItemService inspectionTaskTemplateSamplingItemService;

    @PostMapping("/getInspectionTaskByPage")
    public R export(@RequestBody InspectionTaskDto materialDto)
    {
        Page<InspectionTaskVo> inspectionTaskVoPage = inspectionTaskService.getInspectionTaskByPage(materialDto);
        return R.ok(inspectionTaskVoPage);
    }

    /**
     * 填写数据分页
     * @param materialDto
     * @return
     */
    @PostMapping("/getInspectionTaskTemplateSamplingItemByPage")
    public R export(@RequestBody InspectionTaskTemplateSamplingItemDto materialDto)
    {
        return R.ok(inspectionTaskTemplateSamplingItemService.getInspectionTaskTemplateSamplingItemByPage(materialDto));
    }


    @PostMapping("/copyInspectionTaskTemplateSamplingItem")
    public R copyInspectionTaskTemplateSamplingItem(@RequestBody InspectionTaskTemplateSamplingItemDto materialDto)
    {
        inspectionTaskTemplateSamplingItemService.copyInspectionTaskTemplateSamplingItem(materialDto);
        return R.ok("添加成功");
    }


    @PostMapping("/fillInspectionSamplingTaskValue")
    public R fillInspectionSamplingTaskValue(@RequestBody List<InspectionTaskTemplateSamplingItemDto> materialDto)
    {
        inspectionTaskTemplateSamplingItemService.fillInspectionTaskValue(materialDto);
        return R.ok("数据更新成功");
    }
    /**
     * 添加测试数据
     * @param inspectionTaskValueDtos
     * @return
     */
    @PostMapping("/fillInspectionTaskValueExt")
    public R fillInspectionTaskValueExt(@RequestBody List<InspectionTaskValueDto> inspectionTaskValueDtos)
    {
        inspectionTaskService.fillInspectionTaskValueExt(inspectionTaskValueDtos);
        return R.ok("数据更新成功");
    }

    /**
     * 根据ID获取值
     * @param inspectionTaskTemplateSamplingItemDto
     * @return
     */
    @PostMapping("/getFromInspectionTaskTemplateSamplingItemId")
    public R fillInspectionTaskValueExt(@RequestBody InspectionTaskTemplateSamplingItemDto inspectionTaskTemplateSamplingItemDto)
    {
//        inspectionTaskService.getFromInspectionTaskTemplateSamplingItemId(inspectionTaskTemplateSamplingItemDto);
        return R.ok(inspectionTaskService.getFromInspectionTaskTemplateSamplingItemId(inspectionTaskTemplateSamplingItemDto));
    }

    @PostMapping("/addInspectionTask")
    public R addTest(@RequestBody InspectionTaskDto materialDto) throws ParseException {
        inspectionTaskService.addInspectionTask(materialDto);
        return R.ok("添加成功");
    }


    @PostMapping("/addInspectionTasks")
    public R addTest(@RequestBody List<InspectionTaskDto> inspectionTaskDtos) throws ParseException {
        inspectionTaskService.addInspectionTasks(inspectionTaskDtos);
        return R.ok("添加成功");
    }


    @PostMapping("/delInspectionTask")
    public R delTest(@RequestBody InspectionTaskDto materialDto){
        inspectionTaskService.delInspectionTask(materialDto);
        return R.ok("删除成功");
    }

    @PostMapping("/updateInspectionTask")
    public R updateTest(@RequestBody InspectionTaskDto materialDto){
        inspectionTaskService.updateInspectionTask(materialDto);
        return R.ok("锁定成功");
    }

    @PostMapping("/getValueTmpByTaskId")
    public R getValueTmpByTaskId(@RequestBody InspectionTaskDto materialDto){
        return R.ok(inspectionTaskService.getValueTmpByTaskId(materialDto));
    }

    @PostMapping("/fillInspectionTaskValue")
    public R fillInspectionTaskValue(@RequestBody List<InspectionTaskValueDto> materialDto){
        inspectionTaskService.fillInspectionTaskValue(materialDto);
        return R.ok("添加成功");
    }

    @PostMapping("/downloadTmpData")
    public void exportStageDnHead(HttpServletResponse response, InspectionTaskDto inspectionTaskDto) throws IOException  {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("StageDnHead", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        InputStream is1 = this.getClass().getClassLoader().getResourceAsStream("excel/tmp3.xlsx");
        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(is1).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
            /**
             * 强制换行设计,数据量多了慎用
             */
//            FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
//            excelWriter.fill(new FillWrapper("deHeads", dnHeadExes), fillConfig, writeSheet);
            Map<String, Object> map = inspectionTaskService.getTmpValueById(inspectionTaskDto);
//            map.put("value1", "value1");
//            map.put("value2", "value2");
            excelWriter.fill(map, writeSheet);
            excelWriter.finish();
        }
    }
}
